package org.visionlibrary.image.filters.edge;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;
import org.visionlibrary.image.model.ConvolutionKernel;
import org.visionlibrary.image.model.KernelFilter;


public abstract class CompassEdgeDetector extends AbstractFilter {
	protected ConvolutionKernel[] kernels;
	
	@Override
	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		ImageFactory ifactory = new ImageFactory();
		
		KernelFilter kf = new KernelFilter();
		TiledImage[] dests = new TiledImage[8];
		for(int i=0; i < dests.length; i++) {
			dests[i] = ifactory.createCompatibleImage(src);
			kf.setKernel(kernels[i]);
			kf.applyFilter(src, dests[i]);
		}
		
		int maxX = src.getWidth();
		int maxY = src.getHeight();
		for (int ch = 0; ch < src.getNumBands(); ch++)
			for (int x = 0; x < maxX; x++)
				for (int y = 0; y < maxY; y++) {
					int G = Integer.MIN_VALUE;
					for(int i=0; i <dests.length; i++) {
						int tmp = dests[i].getSample(x, y, ch);
						if(tmp > G)
							G = tmp;
					}
					dest.setSample(x, y, ch, G);
				}
		
		return dest;
	}
	
	public ConvolutionKernel[] getKernels() {
		return kernels;
	}
	
	public void setKernels(ConvolutionKernel[] kernels) {
		this.kernels = kernels;
	}
}
